<?php

/**
 * @author PeoneEr
 */
require_once "core/db.php";

DB::getInstance()->openConnectionToGS();

class Mlegion extends DB {
    private static $_instance = NULL;

    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }
    protected function getLegionID($nick) {
        /**
         *  Получаем ID легиона
         * @param string $nick
         * @return int legion id
         */
        $getLegionIDQuery = "SELECT `legion_id` FROM `legion_members` WHERE `nickname` = '$nick'";
        $fetchLegionID = mysql_fetch_assoc(DB::getInstance()->sql($getLegionIDQuery));
        return $fetchLegionID['legion_id'];
    }

    protected function getPlayerId($nick) {
        /**
         *  Получаем ID игрока (на всякий случай)
         * @param string $nick
         * @return id игрока
         */
        $getPlayerIdQuery = "SELECT `id` FROM `players` WHERE `name` = '$nick' ";
        $fetchPlayerId = mysql_fetch_assoc(DB::getInstance()->sql($getPlayerIdQuery));
        return $fetchPlayerId['player_id'];
    }

    protected function getLegionName($id) {
        /**
         *  Получаем имя легиона
         * @param int $id
         * @return string имя легиона
         */
        $getLegionNameQuery = "SELECT `name` FROM `legions` WHERE `id` = '$id'";
        $fetchLegionName = mysql_fetch_assoc(DB::getInstance()->sql($getLegionNameQuery));
        return $fetchLegionName['name'];
    }

    protected function getLegionStatusPlayer($nick) {
        /**
         *  Получаем статус игрока в легионе
         * @param string $nick
         * @return string статус
         */
        $getLegionStatusPlayerQuery = "SELECT `rank` FROM `legion_members` WHERE `nickname` = '$nick'";
        $fetchLegionStatusPlayer = mysql_fetch_assoc(DB::getInstance()->sql($getLegionStatusPlayerQuery));
        switch ($fetchLegionStatusPlayer['rank']) {
            case LEGIONARY: $status = "Легат";
                break;
            case CENTURION: $status = "Центурион";
                break;
            case BRIGADE_GENERAL: $status = "Бригадный генерал";
                break;
            default: $status = "Ошибка";
        }
        return $status;
    }

    protected function getCharsOnAccount() {
        /**
         *  Получаем ники чаров на аккаунте
         */
        $getCharsOnAccountQuery = "SELECT `name` FROM `players` WHERE `account_name` = '$_SESSION[name]'";
        while ($fetchCharsOnAccount = mysql_fetch_assoc(DB::getInstance()->sql($getCharsOnAccountQuery))) {
            $players[] = $fetchCharsOnAccount['name'];
        }
        return $players;
    }

}

?>
